/*
   Copyright (c) 2019 Shanghai Xuanzi Technology Co. Ltd https://xuanzi.ltd
   XZMindMap is licensed under the Mulan PSL v1.
   You can use this software according to the terms and conditions of the Mulan PSL v1.
   You may obtain a copy of Mulan PSL v1 at:
      http://license.coscl.org.cn/MulanPSL
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
   PURPOSE.
   See the Mulan PSL v1 for more details.

*/

package xuanzi.openmind.renders;

import xuanzi.commons.graphics.Canvas;
import xuanzi.commons.graphics.Paint;
import xuanzi.commons.graphics.PointF;
import xuanzi.commons.graphics.BoxF;
import xuanzi.openmind.layout.Box;
import xuanzi.openmind.layout.HBox;
import xuanzi.openmind.layout.VBox;
import xuanzi.openmind.lines.Line;
import xuanzi.openmind.nodes.Handler;
import xuanzi.openmind.nodes.Label;
import xuanzi.openmind.nodes.Media;
import xuanzi.openmind.nodes.Memo;
import xuanzi.openmind.nodes.Node;
import xuanzi.openmind.nodes.Style;
import xuanzi.openmind.nodes.TopicText;
import xuanzi.openmind.scenes.base.Scene;
import xuanzi.openmind.shapes.Shape;

public class RenderBuffer extends Style{
	
	public Node target;
	 

	public static Canvas canvas;

	public Scene scene;
	
	public Box bound = new VBox() ;
	
 
	
	public Line line;
	
	public String topic ;
	
	public Shape shape;
	
	public int align;

	public Box childBound;

	public String label;

	public String memo;
	
	public  PointF frontPoint = new PointF();
	
	public  PointF backPoint = new PointF();

	static float minSize = 28f; 
  
	public HBox northBox = new HBox();
	public HBox southBox = new HBox(); 
	public HBox westBox = new HBox(8,minSize); 
	public HBox eastBox = new HBox(8,minSize); 
	public HBox centerBox = new HBox(minSize,minSize); 
	
	public TopicText textBox  ; 
	public VBox shapeBound = new VBox(); 
	public Box outerNorthBox = new HBox(); 
	public Box outerSouthBox = new HBox(); 
	
	public Handler handlerBox = new Handler();
	
	public Label labelBox ;
	
	public Box labelSpace ;
	
	public Memo memoBox ;
	 

	public static Paint paint = new Paint();
	
	public void reszie() {
		if(textBox!=null) {
			paint.textSize = textSize;
			textBox.setMiniWidth(RenderBuffer.canvas.textWidth(topic, true, paint));
			textBox.setMiniHeight(textSize*1.6f);
		}
		if(labelBox!=null) {
			paint.textSize = (int) (textSize * 0.8f);
			float w = Math.max(textBox.getMiniWidth(), RenderBuffer.canvas.textWidth(label, true, paint))*1.05f;
			labelBox.setMiniWidth(w);
			labelBox.setMiniHeight(textBox.getMiniHeight()*0.8f);
			labelSpace.setMiniWidth(labelBox.getMiniWidth()+minSize);
		}
	}

	
	public void onLayout(Box parent) {
		
		if(target.getMedias()!=null) {
			bound.add(outerNorthBox);
		}
		parent.add(bound);
		bound.clear();
		bound.add(shapeBound);
		//shapeBound.space = 5;
		textBox = new TopicText(target);
		textBox.setMiniWidth(RenderBuffer.canvas.textWidth(topic, true, paint)); 
	 
		shapeBound.add(northBox);
		
		{
			shapeBound.add(centerBox);
			centerBox.add(new Box(2,2));
			centerBox.add(westBox);
			centerBox.add(textBox);
			centerBox.add(eastBox);
			centerBox.add(new Box(14,2));
		}

		shapeBound.add(southBox);
		if(label!=null) {
			labelBox = new Label(target);  
			bound.add(labelBox); 
			labelSpace = new Box(1,1);
			shapeBound.add(labelSpace);
		}
		reszie();
		if(target.getMedias()!=null) {
			bound.add(outerSouthBox);
		}
		
		if(target.getMedias()!=null) {
			for(Media media:target.getMedias()) {
				
				Box pa = westBox;
				if(media.getAlign()==Media.EAST)
					pa = eastBox;
				if(media.getAlign()==Media.NORTH)
					pa = northBox;
				if(media.getAlign()==Media.SOUTH)
					pa = southBox;
				if(media.getAlign()==Media.NorthSpace)
					pa = outerNorthBox;
				if(media.getAlign()==Media.SouthSpace)
					pa = outerSouthBox;
				media.setMiniWidth(textBox.getMiniHeight());
				media.setMiniHeight(textBox.getMiniHeight());
				media.onLayout(pa);
				pa.add(media.getBound());
			}
		}
		if(target.getMemo()!=null) {
			memoBox = new Memo();
			memoBox.setNode(target);
			eastBox.add(memoBox);
		}
	}
	
	public boolean visiable = true;
 
	
	public Box outerBound;

	public void checkVisable(Node node,BoxF viewBox) { 
		visiable = (BoxF.intersects(viewBox,bound));
		if(node.getNodeCount()>0) {
			for(int i=0;i<node.getNodeCount();i++) {
				Node item = node.getNode(i);
				item.renderBuffer.checkVisable(item,viewBox);
			}
		}
	}
}
